// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   change_module_ok.java

package manufacture.design_module;

import include.nseer_cookie.counter;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;

public class change_module_ok extends HttpServlet
{

	public change_module_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 manufacture_db = new nseer_db_backup1(dbApplication);
		nseer_db_backup1 design_db = new nseer_db_backup1(dbApplication);
		counter count = new counter(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		try
		{
			if (manufacture_db.conn((String)dbSession.getAttribute("unit_db_name")) && design_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String design_ID = request.getParameter("design_ID");
				String product_amount = request.getParameter("product_amount");
				int num = Integer.parseInt(product_amount);
				int n = 0;
				for (int i = 0; i < num; i++)
				{
					String tem_using_amount = (new StringBuilder("using_amount")).append(i).toString();
					String using_amount = request.getParameter(tem_using_amount);
					if (!validata.validata(using_amount))
						n++;
				}

				if (n == 0)
				{
					String module_design_ID = request.getParameter("module_design_ID");
					String procedure_name = request.getParameter("procedure_name");
					String register_time = request.getParameter("register_time");
					String register = request.getParameter("register");
					try
					{
						double cost_price_sum = 0.0D;
						for (int i = 0; i < num; i++)
						{
							String tem_product_name = (new StringBuilder("product_name")).append(i).toString();
							String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
							String tem_type = (new StringBuilder("type")).append(i).toString();
							String tem_product_describe = (new StringBuilder("product_describe")).append(i).toString();
							String tem_amount = (new StringBuilder("amount")).append(i).toString();
							String tem_design_balance_amount = (new StringBuilder("design_balance_amount")).append(i).toString();
							String tem_using_amount = (new StringBuilder("using_amount")).append(i).toString();
							String tem_amount_unit = (new StringBuilder("amount_unit")).append(i).toString();
							String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
							String product_name = request.getParameter(tem_product_name);
							String product_ID = request.getParameter(tem_product_ID);
							String type = request.getParameter(tem_type);
							String product_describe = request.getParameter(tem_product_describe);
							String amount = request.getParameter(tem_amount);
							String design_balance_amount = request.getParameter(tem_design_balance_amount);
							String using_amount2 = request.getParameter(tem_using_amount);
							StringTokenizer tokenTO2 = new StringTokenizer(using_amount2, ",");
							String using_amount;
							String using_amount1;
							for (using_amount = ""; tokenTO2.hasMoreTokens(); using_amount = (new StringBuilder(String.valueOf(using_amount))).append(using_amount1).toString())
								using_amount1 = tokenTO2.nextToken();

							String amount_unit = request.getParameter(tem_amount_unit);
							String cost_price2 = request.getParameter(tem_cost_price);
							StringTokenizer tokenTO3 = new StringTokenizer(cost_price2, ",");
							String cost_price;
							String cost_price1;
							for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
								cost_price1 = tokenTO3.nextToken();

							double subtotal = Double.parseDouble(cost_price) * Double.parseDouble(using_amount);
							double amount1 = Double.parseDouble(design_balance_amount) - Double.parseDouble(using_amount);
							cost_price_sum += subtotal;
							int j = i + 1;
							ResultSet rs4 = manufacture_db.executeQuery((new StringBuilder("select * from manufacture_design_procedure_module_details where design_ID='")).append(design_ID).append("' and procedure_name='").append(procedure_name).append("' and product_ID='").append(product_ID).append("'").toString());
							if (!rs4.next())
							{
								String sql1 = (new StringBuilder("insert into manufacture_design_procedure_module_details(design_ID,procedure_name,details_number,product_ID,product_name,type,product_describe,amount,cost_price,amount_unit,subtotal,register,register_time) values ('")).append(design_ID).append("','").append(procedure_name).append("','").append(j).append("','").append(product_ID).append("','").append(product_name).append("','").append(type).append("','").append(product_describe).append("','").append(using_amount).append("','").append(cost_price).append("','").append(amount_unit).append("','").append(subtotal).append("','").append(register).append("','").append(register_time).append("')").toString();
								manufacture_db.executeUpdate(sql1);
								String sql3 = (new StringBuilder("update design_module_details set design_balance_amount='")).append(amount1).append("' where product_ID='").append(product_ID).append("' and design_ID='").append(module_design_ID).append("'").toString();
								design_db.executeUpdate(sql3);
							}
						}

						String sql2 = (new StringBuilder("update manufacture_design_procedure set design_module_change_tag='1' where design_ID='")).append(design_ID).append("'").toString();
						manufacture_db.executeUpdate(sql2);
						String sql3 = (new StringBuilder("update manufacture_design_procedure_details set design_module_change_tag='1',module_subtotal='")).append(cost_price_sum).append("' where design_ID='").append(design_ID).append("' and procedure_name='").append(procedure_name).append("'").toString();
						manufacture_db.executeUpdate(sql3);
						response.sendRedirect((new StringBuilder("manufacture/design_module/change.jsp?design_ID=")).append(design_ID).toString());
					}
					catch (Exception ex)
					{
						ex.printStackTrace();
					}
				} else
				{
					response.sendRedirect((new StringBuilder("manufacture/design_module/change_module_ok_b.jsp?design_ID=")).append(design_ID).toString());
				}
				design_db.commit();
				manufacture_db.commit();
				design_db.close();
				manufacture_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
